# coding:utf-8
import json
import multiprocessing
import time


class Work(object):
    _q: multiprocessing.Queue = None

    def __init__(self, q: multiprocessing.Queue = None):
        self._q = q

    def send(self, message):
        if not isinstance(message, str):
            message = json.dumps(message)
        self._q.put(message)

    def recv(self):
        while 1:
            result = self._q.get()
            try:
                res = json.loads(result)
            except:
                res = result
            print('recv is %s' % res)

    def send_all(self):
        for i in range(20):
            self._q.put(i)
            time.sleep(1)


if __name__ == '__main__':
    q = multiprocessing.Queue()
    work = Work(q)
    send = multiprocessing.Process(target=work.send, args=("{'name': 'yeskip', }",))
    recv = multiprocessing.Process(target=work.recv)
    send_all = multiprocessing.Process(target=work.send_all)

    send_all.start()
    send.start()
    recv.start()
    # 先让send端发送
    # send.join()
    send_all.join()
    recv.terminate()
